// import Toast from 'path/to/@vant/weapp/dist/toast/toast';
import Toast from '../../../miniprogram/miniprogram_npm/@vant/weapp/toast/index';
import {
  province,            //获取省份
  nextRegion,          //获取下级省市区数据
  addAddress,          //添加收货地址
  nextMobileSegment
} from '../../../utils/apifm-wxapi';

// 
Page({
  data: {    //页面的初始数据
    value:'',
    message1:'',
    message2:'',
    message3:'',
    columns: [],
    columns2: [],
    columns3: [],
    show: false,
    show2: false,
    show3: false,
    provincial:'',       //省份
    city:'',             //城市
    county:"",           //区县
    message1:'',         //姓名  
    message2:'',         //手机号码 
    message3:'',         //详细地址 

  },
  onLoad: function (options) {    //生命周期函数--监听页面加载
    this.province()
  },
  //获取省份
  province(){
    province(wx.getStorageSync('token')).then(res=>{
      if (res.code==0) {
        this.setData({
          columns:res.data
        })
      }
    })
  },
  onChange(e) {
    // console.log(e.target.dataset.item)
    this.setData({
      provincial:e.target.dataset.item,
      show:!this.data.show
    })
    this.nextRegion(e.target.dataset.item)
  },
  showPopup() {
    this.setData({ show: true });
  },





  //获取市
  nextRegion(item){
    nextRegion(item.id).then(res=>{
      // console.log(res.data)
      this.setData({
        columns2:res.data
      })
    })
  },
  onChange2(e) {
    this.setData({
      city:e.target.dataset.item,
      show2:!this.data.show2
    })
    // console.log(e.target.dataset.item)
    this.nextMobileSegment(e.target.dataset.item)
  },
  showPopup2(){
    this.setData({ show2: true });
  },


  //获取区县
  nextMobileSegment(item){
    nextRegion(item.id).then(res=>{
      console.log(res)
      this.setData({
        columns3:res.data
      })
    })
  },
  onChange3(e) {
    this.setData({
      county:e.target.dataset.item,
      show3:!this.data.show3
    })
  },
  showPopup3(){
    this.setData({ show3: true });
  },



  //弹出框隐藏
  onClose() {   
    this.setData({ show: false,show2: false,show3: false });
  },
  //收货人
  message1(e){
    // console.log(e.detail)
    this.setData({
      message1:e.detail
    })
  },
  //手机号码
  message2(e){
    // console.log(e.detail)
    this.setData({
      message2:e.detail
    })
  },
  //详细地址
  message3(e){
    // console.log(e.detail)
    this.setData({
      message3:e.detail
    })
  },
  //保存
  save(){
    addAddress({
      address:this.data.message3,
      cityId:this.data.city.id,
      linkMan:this.data.message1,
      districtId:this.data.county.id,
      mobile:this.data.message2,
      provinceId:this.data.provincial.id,
      token:wx.getStorageSync('token')
    }).then(res=>{
      // console.log(res)
      this.setData({
        provincial:'',       //省份
        city:'',             //城市
        county:"",           //区县
        message1:'',         //姓名  
        message2:'',         //姓名  
        message3:'',         //姓名  
      })
    })
  },
  onReady: function () {    //生命周期函数--监听页面初次渲染完成

  },
  onShow: function () {    //生命周期函数--监听页面显示

  },
  onHide: function () {    //生命周期函数--监听页面隐藏

  },
  onUnload: function () {   //生命周期函数--监听页面卸载

  },
  onPullDownRefresh: function () {    //页面相关事件处理函数--监听用户下拉动作

  },
  onReachBottom: function () {  //页面上拉触底事件的处理函数

  },
  onShareAppMessage: function () {   //用户点击右上角分享

  }
})